import db from "../models/index.js";
import {Op} from "sequelize";
import {getNow} from "../utils/utils.js";

let depotRecord = db.depotRecord;
let depotRecordDao = {};

// 增加仓库物品出入库记录 is_out 0添加 1减少
depotRecordDao.add = (goods_id, amount, is_out, remark, t) => {
  let create_time = getNow();
  let is_delete = 0;
  if (!t) {
    return depotRecord.create({
      goods_id, amount, is_out, create_time, is_delete, remark
    });
  }
  return depotRecord.create({
    goods_id, amount, is_out, create_time, is_delete, remark
  }, {transaction: t});
};

// 逻辑删除仓库物品出入库记录
depotRecordDao.delete = (id, t) => {
  if (!t) {
    return depotRecord.update({is_delete: 1}, {where: {id}});
  }
  return depotRecord.update({is_delete: 1}, {where: {id}, transaction: t});
};

// 查询仓库出入库记录 用来查公司的
depotRecordDao.queryByIds = (goodIds) => {
  return depotRecord.findAndCountAll({
    where: {
      goods_id: {
        [Op.in]: goodIds
      },
      is_delete: 0
    },
    raw: true
  });
};

// 查询仓库物品出入库记录
depotRecordDao.queryByGoodsId = (goods_id) => {
  return depotRecord.findAndCountAll({
    where: {
      goods_id,
      is_delete: 0
    },
    raw: true
  });
};

// 查询仓库物品删除的记录 is_delete 0未删除 1已删除
depotRecordDao.queryByGoodsIdAndIsDelete = (goodIds, is_delete) => {
  return depotRecord.findAndCountAll({
    where: {
      goods_id: {
        [Op.in]: goodIds
      },
      is_delete
    },
    raw: true
  });
};

export default depotRecordDao;
